from oauth.config import OAUTH_APP_SETTINGS

class HTMLPage(object):
    WRAPPER = """<html><head>
                  <title>%(title)s</title>
                  <link href="/static/style.css" type="text/css" rel="stylesheet"/>
                  <script type="text/javascript" src="/static/yuiloader_dom_event.js"></script>
                  <script type="text/javascript" src="/static/jquery.min.js"></script>
                  <script type="text/javascript" src="/static/vizualize.js"></script>
                  <script type="text/javascript" src="/static/scripts.js"></script>
                  </head><body>
                  <h1>%(title)s</h1>
                  %(content)s
                  </body></html>
                  """    
   
    TITLE = """API Console"""
        
class StartPage(HTMLPage):
    CONTENT = """
    <p>This application is available as open source under <a href="http://www.opensource.org/licenses/mit-license.php">MIT license</a> at <a href="http://pp-api-test.googlecode.com">pp-api-test.googlecode.com</a>.</p>
    
    <p>You can use it to test API calls to Projectplace.</p>
    
    <p>For more information about Projectplace APIs. <strong>Wait and focus on the ZEN</strong>.</p> 
    
    <p><a href="/oauth/projectplace/login">Login via Projectplace</a></p>"""
    
    def __init__(self):
        self.html = self.WRAPPER % {'title' : self.TITLE, 'content' : self.CONTENT}
        
    def to_html(self):
        return self.html
    
class APITestPage(HTMLPage):
    CONTENT = """ 
    <a class="logout" href="/oauth/projectplace/logout">Logout from Projectplace</a>
    <!--<div id="options-panel">
        <span><a href="#" onclick='$("#options").slideToggle("fast");'">Options &raquo;</a></span>
    </div>-->
    <div id="options">
        <div id="options-content">
            <fieldset>
                <legend>Show results as</legend>
                <label for="textformat"><input type="radio" checked="checked" name="display" value="text" id="textformat">Prettified JSON</label><br/>
                <label for="tableformat"><input type="radio" name="display" value="html" id="tableformat">Nested structure</label><br/>
            </fieldset>
            <fieldset>
                <legend>Caching</legend>
                <p class="info">Uncheck to force new calls to the API.</p>
                <label for="option_caching_on"><input type="checkbox" checked="checked" id="option_caching_on">Active</label>
            </fieldset>
            <fieldset>
                <legend>Avatar link</legend>
                User-ID: <input type="text" id="avatar_user_id" style="width:225px;"><input type="button" value="Show" onclick="displayAvatar('%(current_access_token)s', '%(api_url)s')"><br>
                <span id="avatar_link"></span>
            <div id="avatar_image"></div>
            </fieldset>
            <fieldset>
                <legend>Download document</legend>
                Doc-ID: <input type="text" style="width:75px;" id="document_id"> Version: <input style="width:75px;" type="text" id="document_version_id">
                <input type="button" value="Download" onclick="downloadDocument();"/><br/>
                <span id="document_resource_location"></span>
            </fieldset>
            <fieldset>
                <legend>History <a href="#" onclick="callHistory.clear()">Clear</a></legend>
                <div id="call_history"></div>
            </fieldset>
        </div>
    </div>
    <div>
    <select id="request_verb">
        <option value="GET">GET</option>
        <option value="POST">POST</option>
        <option value="PUT">PUT</option>
        <option value="DELETE">DELETE</option>
    </select>
    /api/v1&nbsp;<input type="text" name="api_path" id="api_path" placeholder="/user/me/profile/" onkeydown="sendOnReturn(event)">&nbsp;.json
    &nbsp;<input id="access_api" type="button" value="Send" onclick="API.sendRequest();"> 
    <a class="helplink" href="http://code.google.com/p/pp-api-test/wiki/PublicAPIList">API List</a>
    </div>
    <input type="button" id="add_parameter_button" onclick="API.addParameterFields();" value="Add parameter">
    <div id="parameters">
    </div>
    
    <div id="jsonOutput"></div>
    """
    
    def __init__(self, handler):
        self.handler = handler
        
    def to_html(self):
        
        return self.WRAPPER % {'title': self.TITLE, 'content' : self._get_content()}
    
    def _get_content(self):
        return self.CONTENT % {'current_access_token' : self._get_access_token_result(),
                               'api_url' : OAUTH_APP_SETTINGS['projectplace']['default_api_prefix']}
        
    def _get_access_token_result(self):
        from oauth.tokens import OAuthAccessToken
        return "%s" % OAuthAccessToken.get_current_token(self.handler)